package cn.jit.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import cn.jit.po.Book;
import cn.jit.po.Exam;
import cn.jit.po.ExamQuestion;
import cn.jit.po.Question;
import cn.jit.po.User;
import cn.jit.po.UserExam;
import cn.jit.po.UserExamVo;
import cn.jit.service.ExamQuestionService;
import cn.jit.service.ExamService;
import cn.jit.service.UserExamService;
import cn.jit.util.PrintUtil;
import cn.jit.util.ResponseObject;
import cn.jit.util.ResponseUtil;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/exam")
public class ExamController {
	
	@Resource
    private ExamService examService;
	
	@Resource
	private UserExamService userExamService;
	
	@Resource
	private ExamQuestionService examQuestionService;
	
	@RequestMapping("")
	public String examCenterPage() {
		Exam exam = new Exam();
		exam.setExamId(1);
		System.out.println(examService.getExams(exam));
		return "exam_center";
	}
	
	@RequestMapping("/all")
	public String examQuestionPage() {
		
		return "exam_center";
	}
	
	@RequestMapping(value="/detail/{examId}",method=RequestMethod.GET)
	public ModelAndView examDetailPage(@PathVariable Integer examId) {
		
			ModelAndView mv = new ModelAndView();
			ExamQuestion examQuestion = new ExamQuestion();
			examQuestion.setExamId(examId);
			List<ExamQuestion> result = examQuestionService.getExamQuestions(examQuestion);
			
			System.out.println(result.size());
			System.out.println("============================");
			System.out.println(result.get(0).getQuestionList());
			System.out.println("============================");
			
			if (result.size() == 1) {
				mv.addObject("examQuestionList", result.get(0).getQuestionList());
			} else {
				mv.addObject("exam", null);
			}
			
			mv.setViewName("exam_detail");
			return mv;
	}
	
	@RequestMapping("/back_addOne")
	public void addExam(HttpServletResponse response) {
		System.out.println("addExam()开始");
		Exam exam = new Exam();
		exam.setStartTime(new Date());
		exam.setIsActive(1);
		exam.setIsSubmit(0);
		
		// 影响行数
		Integer addResult = examService.addExam(exam);
		// 自增ID
		Integer autoIncrId = exam.getExamId();
		
		if (addResult > 0) {
			JSONObject respJson = JSONObject.fromObject(new ResponseObject(200,true,"新增成功!",autoIncrId));
			ResponseUtil.writeJson(response, respJson);
			return ;
		} else {
			JSONObject respJson = JSONObject.fromObject(new ResponseObject("新增失败!"));
			ResponseUtil.writeJson(response, respJson);
			return ;
		}
		
	}
	
	@RequestMapping("/back_all")
	public void examQuestion(Exam exam,HttpServletResponse response) {
		
		System.out.println(exam);
		ExamQuestion examQuestion = new ExamQuestion();
		ArrayList<Question> questionList = new ArrayList<Question>();
		Question question1 = new Question();
		question1.setQuestionContent("驾考");
		question1.setUserAnswer("A");
		Question question2 = new Question();
		question2.setQuestionContent("jkjdkfjdskjfkd");
		question2.setUserAnswer("B");
		Question question3 = new Question();
		question3.setQuestionContent("eiwqoeioiero");
		question3.setUserAnswer("C");
		questionList.add(question1);
		questionList.add(question2);
		questionList.add(question3);
		examQuestion.setQuestionList(questionList);
		examQuestion.setExamId(1);
		Gson gson = new GsonBuilder().create();
        String resp = gson.toJson(new ResponseObject(200,true,"success",examQuestion));
        System.out.println(resp);
		JSONObject jsonObject = JSONObject.fromObject(new ResponseObject(200,true,"success",examQuestion));
		ResponseUtil.writeJson(response, jsonObject);
		return ;
	}
	
	@RequestMapping("/back_detail")
	public void examQuestionDetail(ExamQuestion examQuestion,HttpServletResponse response) {
		ArrayList<ExamQuestion> examQuestionList = (ArrayList<ExamQuestion>) examQuestionService.getExamQuestions(examQuestion);
		Gson gson = new GsonBuilder().create();
        String resp = gson.toJson(new ResponseObject(200,true,"success",examQuestionList));
        System.out.println(resp);
		JSONObject jsonObject = JSONObject.fromObject(new ResponseObject(200,true,"success",examQuestionList));
		ResponseUtil.writeJson(response, jsonObject);
		return ;
	}
	
	@RequestMapping(value="back_finish",method=RequestMethod.POST)
	public void finishPapaer(@RequestBody ExamQuestion examQuestion,
			HttpServletResponse response) {
		System.out.println(examQuestion);
		
		Integer examId = examQuestion.getExamId();
		
		List<Question> qsList = examQuestion.getQuestionList();
		int rightCount = 0;
		for (Question qs : qsList) {
			if (qs.getUserAnswer().equals(qs.getQuestionAnswer())) {
				qs.setIsRight(1);
				rightCount++;
			} else {
				qs.setIsRight(0);
			}
		}
		
		// 1. 修改在dsts_exam
		Exam exam = new Exam();
		exam.setExamId(examId);
		exam.setEndTime(new Date());
		exam.setIsActive(1);
		exam.setIsSubmit(1);
		exam.setScore(rightCount);
		
		int updateExamRes = examService.updateExam(exam);
		
		if (updateExamRes == 0) {
			ResponseUtil.writeJson(response, JSONObject.fromObject(new ResponseObject("交卷失败!")));
			return ;
		}
		
		// 2. 在dsts_exam_question中新增多条记录
		int addExamQuestionRes = examQuestionService.addExamQuestion(examQuestion);
		PrintUtil.debugInfo("ExamController.finishPapaer()", "新增考试问题表:", addExamQuestionRes);
		
		if (addExamQuestionRes != examQuestion.getQuestionList().size()) {
			ResponseUtil.writeJson(response, JSONObject.fromObject(new ResponseObject("新增考试问题失败!")));
			return ;
		}
		
		ResponseUtil.writeJson(response, JSONObject.fromObject(new ResponseObject(200,true,"交卷成功",rightCount)));
		return ;
	}
	
	@RequestMapping(value="test",method=RequestMethod.POST)
	public void test(@RequestBody ExamQuestion examQuestion,
			HttpServletResponse response) {
		System.out.println(examQuestion);
		ExamQuestion eq = new ExamQuestion();
		eq.setExamId(1);
		eq.setQuestionList(examQuestion.getQuestionList());
		examQuestionService.addExamQuestion(eq);
		
		ResponseUtil.writeJson(response, JSONObject.fromObject(new ResponseObject("success")));
		return ;
	}
	
}
